package com.yuyu.commons.config.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

/**
 * @Description: 解决 seata 与 zipkin 整合时因 SeataFeignClientAutoConfiguration 和 TraceFeignClientAutoConfiguration 都创建 Bean:feignHystrixBuilder 冲突问题
 */
@Component
@ConditionalOnClass({RequestInterceptor.class,GlobalTransactional.class})
public class SetSeataInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate request) {
        String xid = RootContext.getXID();
        if (StringUtils.isNotEmpty(xid)) {
            request.header(RootContext.KEY_XID, xid);
        }
    }
}
